# 爬虫版
import requests
import time
import json
class BlackWidow_BaiDu:
    def __init__(self):
        # 域名
        self.domain = "https://fanyi.baidu.com/"
        # 请求头
        self.headers = {
            'Accept': '*/*',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Origin': 'https://fanyi.baidu.com',
            'Pragma': 'no-cache',
            'Referer': 'https://fanyi.baidu.com/mtpe-individual/multimodal',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0',
            'sec-ch-ua': '"Chromium";v="136", "Microsoft Edge";v="136", "Not.A/Brand";v="99"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
        }
        self.cookies = {
            'BAIDUID_BFESS': '63F78A5BEA251F18368CCB4C2680F468:FG=1',
            'BDUSS': 'kFURmtQejVZMUo3emtuZ3FTNG9IU1R5NzJ5ZWlmdDZjbjR-cUFoZm5nMFVGMFJvSVFBQUFBJCQAAAAAAQAAAAEAAAAgUlOMyMjH6bXEeXkxMjE2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSKHGgUihxoQ',
            'BDUSS_BFESS': 'kFURmtQejVZMUo3emtuZ3FTNG9IU1R5NzJ5ZWlmdDZjbjR-cUFoZm5nMFVGMFJvSVFBQUFBJCQAAAAAAQAAAAEAAAAgUlOMyMjH6bXEeXkxMjE2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSKHGgUihxoQ',
            'Hm_lvt_64ecd82404c51e03dc91cb9e8c025574': '1746956180',
            'BIDUPSID': '63F78A5BEA251F18368CCB4C2680F468',
            'PSTM': '1747936315',
            'H_PS_PSSID': '61027_61675_62325_62718_62832_62968_63148_63188_63194_63164_63210_63242_63249_63327',
            'BA_HECTOR': '0l848h0ha12h0kaka1a40la1a4a52h1k2up1u24',
            'ZFY': 'ycPzqVSK5I4urUEiTajGM8w4ks8ABLT0uP8pZ2kH8Ns:C',
            'ab_sr': '1.0.1_MjJhNjNhNWZlYWVkYzIxMWI2YzVmNzNiYWM3ZWY4YzRkNzZiNzk2MmMyZDM0MGYzNWVkMjY0YTUyZWJmMjk0ZThlMTE2MTJkNmI5NGEyNDZiNzlhMzAwNTBmZmViMGIyNDkxYzBiN2I2MDJmYWUwZjYxY2Q2YWE1OTBmMjdlNmU5YzgyMDY0NTgyMjNkY2U1YWEwZTc2NTk4YjkzMTQ4Zg==',
            'RT': '"z=1&dm=baidu.com&si=b0d61e5d-1cc0-4fb4-82f8-857e8395e1eb&ss=mb0bjetc&sl=1&tt=1p6&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=2h5"',
        }


    
    # 获取翻译语言(判断用户翻译文本的语种)
    def get_translate_language(self, text):
        data = {
            'query': text,
        }

        response = requests.post('https://fanyi.baidu.com/langdetect', headers=self.headers, cookies=self.cookies,data=data)
        return response.json()

    # 获取翻译文本相似的文本
    def get_sug_text(self, text):
        data = {
            'kw': text,
        }

        response = requests.post('https://fanyi.baidu.com/sug', cookies=self.cookies,headers=self.headers, data=data)
        return response.json()
    
    # 翻译
    def translate(self, text, from_lang='zh', to_lang='en'):
        headers = {
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'Acs-Token': '1747846808417_1747885218948_1VaGetpI2EomGwqOJsgPwmP+UGBLmwY034pDMgd2o6dTMz9ZLn5b6h+G74++XKUbxfZ2LPQnR+dG68OHM4ljbtOc9mT20txzb6X6ndIYmsZdQoEe8Ow2jpdh7RcLLnAbxki54bsGzqjffOXwGe8v680bkZnl9q7IMcxHYSSLIzM42y2ZV5X4+/ocuWfQScC6vGZ/hgvVklGI6/7LcLNYwwmJf8T8X81amEPH3WIQhbykazTVCioikHs82sKLBX1u0S4f6+KxAHGK24dB4W63QC6xqYj47FymCcJrPkSE0JfFWO64ErsBJ7nUrCivDGQzb5fiO2DpTe6AHwDoRSyaPgKngzrddTXie5ey6aEEUuTtM32wSnPO+8XwKm+VpxfnRKB+zwgmXr3o1G/q6EH1vA9RTo4MH7iSlQOx5W6wBNaOXzrZinIt1dABfha7ljSeljwQtZ0HiahyBAP4iUw0hQ==',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
            'Content-Type': 'application/json',
            'Origin': 'https://fanyi.baidu.com',
            'Pragma': 'no-cache',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0',
            'accept': 'text/event-stream',
            'sec-ch-ua': '"Chromium";v="136", "Microsoft Edge";v="136", "Not.A/Brand";v="99"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            # 'Cookie': 'BAIDUID_BFESS=63F78A5BEA251F18368CCB4C2680F468:FG=1; BDUSS=kFURmtQejVZMUo3emtuZ3FTNG9IU1R5NzJ5ZWlmdDZjbjR-cUFoZm5nMFVGMFJvSVFBQUFBJCQAAAAAAQAAAAEAAAAgUlOMyMjH6bXEeXkxMjE2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSKHGgUihxoQ; BDUSS_BFESS=kFURmtQejVZMUo3emtuZ3FTNG9IU1R5NzJ5ZWlmdDZjbjR-cUFoZm5nMFVGMFJvSVFBQU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5Mzk1Y2Q2ZGUxYmNiYmMxZjliY2ExMQ==; RT="z=1&dm=baidu.com&si=b0d61e5d-1cc0-4fb4-82f8-857e8395e1eb&ss=mauzeiqc&sl=1&tt=1hr&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=3ubik8"',
        }

        json_data = {
            'query': text,
            'from': from_lang,
            'to': to_lang,
            'reference': '',
            'corpusIds': [],
            'needPhonetic': False,
            'domain': 'common',
            'milliTimestamp': int(time.time() * 1000),
        }

        response = requests.post('https://fanyi.baidu.com/ait/text/translate',cookies=self.cookies,headers=headers, json=json_data, stream=True)


      
        for line in response.iter_lines():
            if line:
                line = line.decode('utf-8')
                if line.startswith('data:'):
                    line = line.replace('data:', '')
                    json_data = json.loads(line)
                    if json_data['errno'] == 0:
                        if json_data['data']['event'] == "Translating":
                            return json_data['data']['list']


                    


if __name__ == '__main__':
    black_widow = BlackWidow_BaiDu()
    print(black_widow.translate('你叫什么名字'))

